package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.manor.ManorConfig;
import com.yanqu.road.entity.config.manor.ManorGuestConfig;
import com.yanqu.road.entity.config.manor.ManorTechnologyConfig;
import com.yanqu.road.entity.config.manor.ManorUpgradeConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/5/21 12:23
 */
public class ManorConfigDaoImpl extends TempDao {

    public static String getManorConfigSql = "select * from t_p_manor";
    public Map<Integer, ManorConfig> getManorConfigMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ManorConfig> configMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getManorConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                ManorConfig config = new ManorConfig();
                config.setId(rs.getInt("id"));
                config.setPosition(rs.getInt("position"));
                config.setType(rs.getInt("type"));
                config.setClassification(rs.getInt("classification"));
                config.setUnlock(rs.getInt("unlock"));
                config.setProduction(rs.getInt("productionId"));
                String productionPar = rs.getString("productionPar");
                String[] productionArr = productionPar.split("\\|");
                List<Integer> list = new ArrayList<>();
                for (String productionStr : productionArr) {
                    list.add(Integer.parseInt(productionStr));
                }
                config.setProductionPar(list);
                config.setUpgradePar(rs.getLong("upgradePar"));
                config.setPlantRewards(rs.getString("plantRewards"));
                config.setSkill(rs.getString("skill"));
                config.setUnlockConsume(rs.getString("unlockConsume"));
                configMap.put(config.getId(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }

    public static String getManorGuestConfigSql = "select * from t_p_manor_guest";
    public Map<Integer, ManorGuestConfig> getManorGuestConfigMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ManorGuestConfig> configMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getManorGuestConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                ManorGuestConfig config = new ManorGuestConfig();
                config.setId(rs.getInt("id"));
                config.setSkill(rs.getInt("skill"));
                config.setUnlockReward(rs.getInt("unlockReward"));
                config.setUnlockType(rs.getInt("unlockType"));
                config.setUnlockPar(rs.getString("unlockPar"));
                config.setBelongId(rs.getInt("belongId"));
                configMap.put(config.getId(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }

    public static String getManorTechnologyConfigSql = "select * from t_p_manor_technology";
    public Map<Integer, ManorTechnologyConfig> getManorTechnologyConfigMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ManorTechnologyConfig> configMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getManorTechnologyConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                ManorTechnologyConfig config = new ManorTechnologyConfig();
                config.setId(rs.getInt("id"));
                config.setUnlock(rs.getInt("unlock"));
                config.setUpgradeItem(rs.getInt("upgradeItem"));

                String unlockConsumePar = rs.getString("unlockConsume");
                String[] unlockConsumeArr = unlockConsumePar.split("\\|");
                List<String> cList = Arrays.asList(unlockConsumeArr);
                config.setUnlockCost(cList);

                String volumesIdPar= rs.getString("volumesIdList");
                String[] volumesIdArr = volumesIdPar.split("\\|");
                List<Integer> list = new ArrayList<>();
                for (String volumesIdStr : volumesIdArr) {
                    list.add(Integer.parseInt(volumesIdStr));
                }
                config.setVolumesIdList(list);

                String volumeObjPar= rs.getString("volumeObjList");
                String[] volumeObjArr = volumeObjPar.split("\\|");
                List<String> vList = Arrays.asList(volumeObjArr);
                config.setVolumeObjList(vList);

                config.setVolumeUpgradeList(rs.getString("volumeUpgradeList"));

                configMap.put(config.getId(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }
    public static String getManorUpgradeConfigSql = "select * from t_p_manor_upgrade";
    public Map<Integer, ManorUpgradeConfig> getManorUpgradeConfigMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ManorUpgradeConfig> configMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getManorUpgradeConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                ManorUpgradeConfig config = new ManorUpgradeConfig();
                config.setId(rs.getInt("id"));
                config.setLevel(rs.getInt("level"));
                config.setConsume(rs.getString("consume"));
                configMap.put(config.getLevel(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }
}
